import Vue from "vue";
import dayjs from 'dayjs';

const workflowStatus = [
  { key: -1, value: '草稿' },
  { key: 0, value: '流转结束' },
  { key: 1, value: '用户取消' },
  { key: 2, value: '管理员取消' },
  { key: 3, value: '流转中' },
  { key: 4, value: '流转中(曾被拒绝过)' },
  { key: 5, value: '未流转' },
  { key: 6, value: '表单失效' }
];

// 数字过滤器
Vue.filter("number", {
  read: function (value, bits = 3) {
    if (value) {
      return (parseFloat(value) || 0).toFixed(bits);
    }

    return value;
  },
  write: function (value) {
    if (value) {
      return parseFloat(value || 0).toFixed(bits);
    }

    return value;
  }
});

// 整数过滤器
Vue.filter("int", {
  read: function (value) {
    if (value) {
      return parseFloat(value).toFixed(0) || 0;
    }

    return value;
  },
  write: function (value) {
    if (value) {
      return parseFloat(value).toFixed(0) || 0;
    }

    return value;
  }
});

Vue.filter('image', val => {
  if (val) {
    let images = JSON.parse(val) || [];

    return images[0]
  }

  return null;
});

Vue.filter('status', val => {
  let rs = '';

  switch ((val || '').toUpperCase()) {
    case 'Y':
      rs = '有效';

      break;
    case 'N':
      rs = '禁用';

      break;

    default:
      rs = '无效';
  }

  return rs;
});

Vue.filter('defaultValue', (item, value) => {
  if (item) {
    return item;
  }

  return value;
});

Vue.filter('date', (value, format) => {
  return dayjs(value).format(format || 'YYYY-MM-DD');
});

Vue.filter('datetime', (value, format) => {
  if (!value) {
    return '';
  }

  return dayjs(value).format(format || 'YYYY-MM-DD HH:mm:ss');
});

Vue.filter('workflowStatus', item => {
  return (workflowStatus.find(w => w.key == item) || { vlaue: '状态未定义' }).value;
});

const typeMappings = {
  string: {
    from: ['text', 'ntext', 'nvarchar', 'varchar', 'nvarchar2', 'varchar2', 'char', 'nchar'],
    nullable: ''
  },
  long: {
    from: ['bigint'],
    nullable: '?'
  },
  int: {
    from: ['int', 'enum'],
    nullable: '?'
  },
  short: {
    from: ['smallint'],
    nullable: '?'
  },
  float: {
    from: ['real'],
    nullable: '?'
  },
  double: {
    from: ['float'],
    nullable: '?'
  },
  decimal: {
    from: ['decimal', 'money', 'numeric', 'smallmoney'],
    nullable: '?'
  },
  byte: {
    from: ['tinyint'],
    nullable: '?'
  },
  'byte[]': {
    from: ['image', 'binary', 'varbinary'],
    nullable: '?'
  },
  bool: {
    from: ['bit'],
    nullable: '?'
  },
  Guid: {
    from: ['uniqueidentifier'],
    nullable: '?'
  },
  DateTime: {
    from: ['date', 'datetime', 'datetime2', 'smalldatetime', 'timestamp'],
    nullable: '?'
  }
};

/**
 * 代码类型
 */
Vue.filter('codeType', (type, nullable, length) => {
  let rs = '';
  let props = Object.keys(typeMappings);

  if (type.includes('(')) {
    type = type.substr(0, type.indexOf('('));
  }

  type = type.toLowerCase();

  for (let prop of props) {
    if (typeMappings[prop].from.includes(type)) {
      rs = prop + ((nullable == 1 || nullable == 'YES') ? typeMappings[prop].nullable : '');
    }
  }

  return rs;
});

Vue.filter('pascal', value => {
  let rs = '';
  let items = value.split('_');

  for (let item of items) {
    if (item && item.length > 1) {
      rs += item[0].toUpperCase() + item.substr(1);
    } else if (item) {
      rs += item[0].toUpperCase();
    }
  }

  return rs;
});